﻿using System;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using System.Drawing;
using Silvermoon.Shapes;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Item which contains another control.
    /// </summary>
    public class ContainerItem : VirtualItem
    {
        private Control control;
        private ControlCollection visibleControls;
        private bool autoHeight = true;


        protected override void Dispose(bool dispose)
        {
            Control = null;
            base.Dispose(dispose);
        }


        /// <summary>
        /// Control to display.
        /// </summary>
        public Control Control
        {
            get { return control; }
            set
            {
                if (control != value)
                {
                    Control old = control;
                    control = value;
                    OnControlChanged(old);
                }
            }
        }

        public override Size Size
        {
            get
            {
                Size size = base.Size;
                if (AutoHeight)
                {
                    size.Height = control != null ? (control.PaddingHeight) : 0;
                }
                return size;
            }
            set
            {
                if (autoHeight) value.Height = Height;
                base.Size = value;
            }
        }

        /// <summary>
        /// True to determine the height depending on the control, otherwise false.
        /// </summary>
        public bool AutoHeight
        {
            get { return autoHeight; }
            set
            {
                if (autoHeight != value)
                {
                    autoHeight = value;
                    OnAutoHeightChanged();
                }
            }
        }

        protected virtual void OnAutoHeightChanged()
        {
            Invalidate(InvalidationFlag.Appearance);
        }


        protected virtual void OnControlChanged(Control old)
        {
            if (old != null)
            {
                VisibleControls.Remove(old);
                old.Dispose();
            }
            if (control != null)
            {
                VisibleControls.Add(control);
            }
            Invalidate(InvalidationFlag.Appearance);
        }

        protected override void PreLayout()
        {

            if (control != null)
            {
                if (CheckFlag(InvalidationFlag.Layout | InvalidationFlag.Size))
                {
                    control.Bounds = GetControlBounds();
                }
            }
            base.PreLayout();
        }

        protected override void OnChildSizeChanged(Control child, Size oldSize)
        {
            if (autoHeight)
            {
                IItemHost listView = Host;
                if (listView != null) listView.SetItemHeight(this.ItemIndex, Height + Margin.Height);
                Invalidate(InvalidationFlag.Layout);
            }            
        }


        protected virtual Rectangle GetControlBounds()
        {
            if (control == null) return Rectangle.Empty;
            Rectangle rect = control.Margin.Layout(control.Bounds, Size);
            return rect;
        }


        protected internal override ControlCollection GetVisibleControls()
        {
            if (visibleControls == null) visibleControls = new ControlCollection(this);
            return visibleControls;
        }

        protected internal override void OnRender(Silvermoon.Core.Renderer renderer)
        {
            var shapes = Shapes;
            int alpha = renderer.Alpha;
            if (shapes != null) renderer.RenderShapes(shapes);

            if (control != null)
            {
                Rectangle scissorBox = renderer.ScissorBox;
                renderer.RenderControl(control);
                renderer.ScissorBox = scissorBox;
            }
            Shape chrome = Chrome;
            if (chrome != null)
            {
                renderer.RenderShape(chrome, alpha);
            }
        }

        protected override void OnSizeChanging(Size oldValue)
        {
            if (oldValue.Height != Height)
            {
                IItemHost listView = Host;
                if (listView != null) listView.SetItemHeight(this.ItemIndex, Height + Margin.Height);
            }
            base.OnSizeChanging(oldValue);
        }

    }
}
